#!/bin/bash
#获取当前的目录
CUR_PATH=$(readlink -f $(dirname $0))
MYSQL_DATA_DIR="" #mysql的数据存放的路径
#判断config.ini是否存在
if [ ! -e $CUR_PATH/config.ini ]; then
    echo "Config File config.ini is not exists..please check..."
    exit 1
fi

#判断日志文件是否存在。如果存在则删除掉
if [ -e $CUR_PATH/disk_format.log ]; then
    rm -f $CUR_PATH/disk_format.log
fi

# 根据上条指令执行结果正确与否输出对应信息
function result_echo() {
    if [ $? -eq 0 ]; then
        echo "$1" | tee -a >>$CUR_PATH/disk_format.log
    else
        echo "$2" | tee -a >>$CUR_PATH/disk_format.log
        exit 1
    fi
}

cat $CUR_PATH/config.ini | sed -n '/\[mysql\]/,/\[.*\]/p' | grep -v '\[.*\]' | grep -v \^\# | grep -v \^[[:space:]]\*\$ >/tmp/mysql_info
cat $CUR_PATH/config.ini | sed -n '/\[mysql\]/,/\[.*\]/p' | grep -v '\[.*\]' | grep -v \^\# | grep -v \^[[:space:]]\*\$ | awk '{print $1}' >/tmp/mysql_hosts
INDEX=0
for host in $(cat /tmp/mysql_hosts); do
    ansible -i /tmp/mysql_hosts $host -m ping
    result_echo "OK...Connect To Host $host Success" "Sorry,Connect To Host $host Failed...Please Check..."
    ansible -i /tmp/mysql_hosts $host -m copy -a 'src=/etc/yum.repos.d/http.repo dest=/etc/yum.repos.d/'
    result_echo "OK...Copy http.repo To $host Success" "Sorry,Copy http.repo To $host Failed...Please Check..."

    ansible -i /tmp/mysql_hosts $host -m shell -a "yum makecache"
    ansible -i /tmp/mysql_hosts $host -m shell -a "yum install mariadb maridb-server -y"
    result_echo "OK...Install mariadb-server Success" "Sorry,Install Mariadb-server Failed...Please Check..."

    STATUS=$(cat /tmp/mysql_info | grep $host | sort | uniq | awk '{print $2}')
    if [[ "$STATUS" != "master" || "$STATUS" == "slave" ]]; then
        echo "Sorry, MySQL Status $STATUS Config Error...Must be 'master|slave' Please Check..."
        exit 1
    else
        ansible -i /tmp/mysql_hosts $host -m lineinfile -a "path=/etc/my.cnf regexp='datadir' line='datadir=$MYSQL_DATA_DIR' insertafter='^\[mysql\]'"
        result_echo "OK...Set datadir=$MYSQL_DATA_DIR in /etc/my.cnf Success" "Sorry,Set datadir in /etc/my.cnf Failed...Please Check..."
        ansible -i /tmp/mysql_hosts $host -m lineinfile -a "path=/etc/my.cnf regexp='server_id' line='server_id=$INDEX' insertafter='^\[mysqld\]'"
        result_echo "OK...Set server_id=$INDEX in /etc/my.cnf Success" "Sorry,Set server_id=$INDEX in /etc/my.cnf Failed...Please Check..."

        if [[ "$STATUS" == "master" ]]; then
            ansible -i /tmp/mysql_hosts $host -m lineinfile -a 'path=/etc/my.cnf regexp="log-bin" line="log-bin=master-bin" insertafter="^\[mysqld\]"'
            result_echo "OK...Set log-bin=master-bin in /etc/my.cnf Success" "Sorry,Set log-bin=master-bin in /etc/my.cnf Failed...Please Check..."
            ansible -i /tmp/mysql_hosts $host -m shell -a "chown mysql:mysql $MYSQL_DATA_DIR"
            result_echo "OK...Modify mysql:mysql For $MYSQL_DATA_DIR Success" "Sorry,Modify mysql:mysql For $MYSQL_DATA_DIR Failed...Please Check..."
            #启动mysql服务
            ansible -i /tmp/mysql_hosts $host -m service -a "name=mariadb state=restarted"
            result_echo "OK...MySQL Server is RUNNING" "Sorry,MySQL Server Start Failed...Please Check..."

            ansible -i /tmp/mysql_hosts $host -m shell -a "mysql -e \"GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'yll'\""
            MASTER_FILENAME=$(mysql -e "show master status\G" | grep File | awk -F:'{print $2}')
            MASTER_POSTION=$(mysql -e "show master status\G" | grep Position | awk -F:'{print $2}')
            MASTER_HOSTIP=$(cat /tmp/mysql_info | akw '$2~/master/{print $1}')
        fi
        if [[ "$STATUS" == "slave" ]]; then
            ansible -i /tmp/mysql_hosts $host -m lineinfile -a 'path=/etc/my.cnf regexp="relay-log" line="relay-log=slave-log" insertafter="^\[mysqld\]"'
            result_echo "OK...Set relay-log=slave-log in /etc/my.cnf Success" "Sorry,Set relay-log=slave-log in /etc/my.cnf Failed...Please Check..."
            ansible -i /tmp/mysql_hosts $host -m shell -a "mysql -e 'CHANGE MASTER TO master_host=\"$MASTER_HOSTIP\",master_user=\"slave_user\",master_password=\"yll\",master_log_file=\"$MASTER_FILENAME\",master_log_pos=\"$MASTER_POSTION\"'"
            ansible -i /tmp/mysql_hosts $host -m shell -a "mysql -e \"start slave\""
        fi

        INDEX=$(expr $INDEX+1)
    fi
done
